%------------------------------------------------------------------
%------------------------------------------------------------------
%   Author: Md. Sazzad Hussain (sazzad.hussain@sydney.edu.au)
%   Learning and Affect Technologies Engineering
%   University of Sydney, 2010
%------------------------------------------------------------------
%------------------------------------------------------------------

% %%generates statistics about self reports
function [statExt]=stat_selfReport(mode,subjectID,filesPath,statdir,opSys)
%Get stats from IAPS self reports

if opSys==1;
    sep='/';
elseif opSys==2
    sep='\';
end

filesDir=dir(filesPath);%dir info for files path
x=1;
for m=1:length(filesDir)
    [path,name,ext,ver] = fileparts(filesDir(m).name);
    for n=1:length(subjectID(:,1))
        if strncmp(subjectID(n,:),name,27)%choose only selected subjects
            if strcmp(ext, '.txt')%.txt files
                loadAnn(x,:)=[filesPath sep name ext];
                x=x+1;
            end
        end
    end
end

mkdir(statdir);

if mode==1 %IAPS mode
    % %self report
    cordself1={'MediumValence-LowArousal'};
    cordself2={'HighValence-LowArousal'};
    cordself3={'MediumValence-MediumArousal'};
    cordself4={'HighValence-MediumArousal'};
    cordself5={'HighValence-HighArousal'};
    cordself6={'MediumValence-HighArousal'};
    cordself7={'LowValence-HighArousal'};
    cordself8={'LowValence-MediumArousal'};
    cordself9={'LowValence-LowArousal'};
    cord1=0;    %co-ordinate1
    cord2=0;    %co-ordinate2
    cord3=0;    %co-ordinate3
    cord4=0;    %co-ordinate4
    cord5=0;    %co-ordinate5
    cord6=0;    %co-ordinate6
    cord7=0;    %co-ordinate7
    cord8=0;    %co-ordinate8
    cord9=0;    %co-ordinate9
    %
    for j=1:(x-1)
        fid = fopen(loadAnn(j,:));%load annotation
        C = textscan(fid,'%s%s%s%s%f%s','delimiter', ',');
        fclose(fid);
        k = 2;%start index of file
        while k <= (length(C{5})-2)
            %extracting self reports
            switch C{5}(k)
                case 1
                    cord1=cord1+1;
                case 2
                    cord2=cord2+1;
                case 3
                    cord3=cord3+1;
                case 4
                    cord4=cord4+1;
                case 5
                    cord5=cord5+1;
                case 6
                    cord6=cord6+1;
                case 7
                    cord7=cord7+1;
                case 8
                    cord8=cord8+1;
                case 9
                    cord9=cord9+1;
            end
            k=k+1;
        end
    end
    
elseif mode==2 %AutoTutor mode
    indexNum=1;
    valSelf=[];
    arSelf=[];
    dimSelf=[];
    catgry=[];
    %file I/O for mapping data
    fidAT=fopen([statdir sep 'mapping1.txt'],'a');%open file
    fprintf(fidAT,'Index,');%image/seq
    fprintf(fidAT,'Dimension,');%image/seq
    fprintf(fidAT,'Valence,');%image/seq
    fprintf(fidAT,'Arousal,');%image/seq
    fprintf(fidAT,'Category\n');%image/seq
    
    % %self report
    cordself1={'MediumValence-LowArousal'};
    cordself2={'HighValence-LowArousal'};
    cordself3={'MediumValence-MediumArousal'};
    cordself4={'HighValence-MediumArousal'};
    cordself5={'HighValence-HighArousal'};
    cordself6={'MediumValence-HighArousal'};
    cordself7={'LowValence-HighArousal'};
    cordself8={'LowValence-MediumArousal'};
    cordself9={'LowValence-LowArousal'};
    cord1=0;    %co-ordinate1
    cord2=0;    %co-ordinate2
    cord3=0;    %co-ordinate3
    cord4=0;    %co-ordinate4
    cord5=0;    %co-ordinate5
    cord6=0;    %co-ordinate6
    cord7=0;    %co-ordinate7
    cord8=0;    %co-ordinate8
    cord9=0;    %co-ordinate9
    
    catself1={'boredom'};
    catself2={'confusion'};
    catself3={'curiosity'};
    catself4={'delight'};
    catself5={'flow'};
    catself6={'surprise'};
    catself7={'frustration'};
    catself8={'neutral'};
    
    cat1=0;
    cat2=0;
    cat3=0;
    cat4=0;
    cat5=0;
    cat6=0;
    cat7=0;
    cat8=0;
    
    for j=1:(x-1)
        fid = fopen(loadAnn(j,:));%load annotation
        C = textscan(fid,'%s','delimiter', '\n');
        k=1;
        while k <=length(C{1})
            if mod(k,5)==3%category
                cat=regexprep(C{1}(k),'''','');
                cat=char(cat);
                catgry=cat;
                switch cat
                    case 'boredom'
                        cat1=cat1+1;
                    case 'confusion'
                        cat2=cat2+1;
                    case 'curiosity'
                        cat3=cat3+1;
                    case 'delight'
                        cat4=cat4+1;
                    case 'flow'
                        cat5=cat5+1;
                    case 'surprise'
                        cat6=cat6+1;
                    case 'frustration'
                        cat7=cat7+1;
                    case 'neutral'
                        cat8=cat8+1;
                end%switch
                
            elseif mod(k,5)==4%dimension
                dim=regexprep(char(C{1}{k}),'''' , '');
                dim=str2num(dim);
                switch dim
                    case 1
                        valSelf={'MediumValence'};
                        arSelf={'LowArousal'};
                        dimSelf={'MediumValence-LowArousal'};
                        cord1=cord1+1;
                    case 2
                        valSelf={'HighValence'};
                        arSelf={'LowArousal'};
                        dimSelf={'HighValence-LowArousal'};
                        cord2=cord2+1;
                    case 3
                        valSelf={'MediumValence'};
                        arSelf={'MediumArousal'};
                        dimSelf={'MediumValence-MediumArousal'};
                        cord3=cord3+1;
                    case 4
                        valSelf={'HighValence'};
                        arSelf={'MediumArousal'};
                        dimSelf={'HighValence-MediumArousal'};
                        cord4=cord4+1;
                    case 5
                        valSelf={'HighValence'};
                        arSelf={'HighArousal'};
                        dimSelf={'HighValence-HighArousal'};
                        cord5=cord5+1;
                    case 6
                        valSelf={'MediumValence'};
                        arSelf={'HighArousal'};
                        dimSelf={'MediumValence-HighArousal'};
                        cord6=cord6+1;
                    case 7
                        valSelf={'LowValence'};
                        arSelf={'HighArousal'};
                        dimSelf={'LowValence-HighArousal'};
                        cord7=cord7+1;
                    case 8
                        valSelf={'LowValence'};
                        arSelf={'MediumArousal'};
                        dimSelf={'LowValence-MediumArousal'};
                        cord8=cord8+1;
                    case 9
                        valSelf={'LowValence'};
                        arSelf={'LowArousal'};
                        dimSelf={'LowValence-LowArousal'};
                        cord9=cord9+1;
                end%switch
            elseif mod(k,5)==0%write data
                fprintf(fidAT, '%s,',num2str(indexNum));%image/seq
                fprintf(fidAT, '%s,',char(dimSelf));%image/seq
                fprintf(fidAT, '%s,',char(valSelf));%image/seq
                fprintf(fidAT, '%s,',char(arSelf));%image/seq
                fprintf(fidAT, '%s\n',char(catgry));%image/seq
                indexNum=indexNum+1;
            end%AutoTutor fields
            k=k+1;
        end%file
    end%subject
    fclose(fid);
    fclose(fidAT);
end%mode

%self report stat
disp('Self Report - Dimension')
disp([char(cordself1),',',num2str(cord1)])
disp([char(cordself2),',',num2str(cord2)])
disp([char(cordself3),',',num2str(cord3)])
disp([char(cordself4),',',num2str(cord4)])
disp([char(cordself5),',',num2str(cord5)])
disp([char(cordself6),',',num2str(cord6)])
disp([char(cordself7),',',num2str(cord7)])
disp([char(cordself8),',',num2str(cord8)])
disp([char(cordself9),',',num2str(cord9)])

if mode==2
    disp('                 ')
    disp('Self Report - Category')
    disp([char(catself1),',',num2str(cat1)])
    disp([char(catself2),',',num2str(cat2)])
    disp([char(catself3),',',num2str(cat3)])
    disp([char(catself4),',',num2str(cat4)])
    disp([char(catself5),',',num2str(cat5)])
    disp([char(catself6),',',num2str(cat6)])
    disp([char(catself7),',',num2str(cat7)])
    disp([char(catself8),',',num2str(cat8)])
    disp('                 ')
    mm=cat1+cat2+cat3+cat4+cat5+cat6+cat7+cat8;
    disp(num2str(mm))
    disp(num2str(cat1/mm))
    disp(num2str(cat2/mm))
    disp(num2str(cat3/mm))
    disp(num2str(cat4/mm))
    disp(num2str(cat5/mm))
    disp(num2str(cat6/mm))
    disp(num2str(cat7/mm))
    disp(num2str(cat8/mm))
end
clear all;
statExt=1;
